package worker.control;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import worker.model.service.WipService;
import worker.model.vo.DefectsVO;
import worker.model.vo.ItemInfoVO;
import worker.model.vo.ItemVO;
import worker.model.vo.OperationsVO;

public class WipController extends MultiActionController {
	private WipService wipService;
	
	public void setWipService(WipService wipService) {
		this.wipService = wipService;
	}
	/**
	 * 120526 조철희
	 * wip 메뉴에서 Item을 등록하고 리스트를 가져옴
	 */
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response){
		System.out.println("main join...session : "+request.getSession().getAttribute("vo"));
		HashMap map = new HashMap();
		ArrayList<HashMap> list = wipService.getItemInfoList();
		ArrayList<HashMap> list2 = wipService.getOperationList();
		ArrayList<HashMap> list3 = wipService.getDefectsList();
		map.put("material",list);
		map.put("operation",list2);
		map.put("defect",list3);
		System.out.println(map);
		return new ModelAndView("wip.wip_main","map",map);
	}
	/**
	 * 120526 조철희
	 * wip 메뉴에서 Item을 등록하고 리스트를 가져옴
	 */
	public ModelAndView addItemInfo(HttpServletRequest request, HttpServletResponse response, ItemInfoVO itemVO){
		System.out.println("addItemInfo join..itemInfoVO : "+itemVO);
		boolean flag = wipService.addItemInfo(itemVO);
		if(!flag){
			return new ModelAndView("JsonView","msg","N");
		}
		ArrayList<HashMap> list = wipService.getItemInfoList();
		return new ModelAndView("JsonView","list",list);
	}
	/**
	 * 120526 조철희
	 * wip 메뉴에서 Operation을 등록하고 리스트를 가져옴
	 */
	public ModelAndView addOperation(HttpServletRequest request, HttpServletResponse response, OperationsVO opVO){
		System.out.println("addOperation join..OperationsVO : "+opVO);
		boolean flag = wipService.addOperation(opVO);
		if(!flag){
			return new ModelAndView("JsonView","msg","N");
		}
		ArrayList<HashMap> list = wipService.getOperationList();
		return new ModelAndView("JsonView","list",list);
	}
	/** 
	 * 120526 조철희
	 * wip 메뉴에서 Defect을 등록하고 리스트를 가져옴
	 */
	public ModelAndView addDefect(HttpServletRequest request, HttpServletResponse response, DefectsVO dVO){
		System.out.println("addDefect join..DefectsVO : "+dVO);
		boolean flag = wipService.addDefect(dVO);
		if(!flag){
			return new ModelAndView("JsonView","msg","N");
		}
		ArrayList<HashMap> list = wipService.getDefectsList();
		return new ModelAndView("JsonView","list",list);
	}
	/**
	 * 이주연... ^^
	 * 아이템을 등록
	 */
	public ModelAndView updateMaterial(HttpServletRequest request, HttpServletResponse response, ItemVO ivo){
		
		System.out.println("이주연: WipController..updateMaterial(): ");
		// 아이템 등록
		ItemInfoVO  iivo1= new ItemInfoVO("ICE",ivo.getItem_id_ice());
		ItemInfoVO  iivo2= new ItemInfoVO("PC",ivo.getItem_id_pc());
		ItemInfoVO  iivo3= new ItemInfoVO("RB",ivo.getItem_id_rb());
		ItemInfoVO  iivo4= new ItemInfoVO("TP", ivo.getItem_id_tp());
		
		this.wipService.updateMaterial(iivo1);
		this.wipService.updateMaterial(iivo2);
		this.wipService.updateMaterial(iivo3);
		this.wipService.updateMaterial(iivo4);
		
		// 업데이트 된 ItemInfo 리스트를 갖고온다. 
		ArrayList<HashMap> list= (ArrayList<HashMap>)this.wipService.getItemInfoList();
		System.out.println(list.toString());
		return new ModelAndView("JsonView","list",list);
	}
}
